<template>
  <!-- 条件节点 -->
  <div class="ep-node-condition">
    <div v-if="!props.node.isDefault">{{ node.nodeName }}</div>
    <div v-else>其他条件进入此流程</div>
  </div>
</template>

<script setup name="conditionNode">
import { inject } from "vue";
import { KEY_VALIDATOR } from "../flow";

const props = defineProps({
  tempNodeId: {
    // 临时节点ID
    type: String
  },
  node: {
    // 传入的流程配置数据
    type: Object,
    default: () => {}
  }
});

// 获取流程验证器实例
const validator = inject(KEY_VALIDATOR);

// 注册验证器
validator.register(props.tempNodeId, () => {
  let valid = true;
  if (!props.node.isDefault) {
    const expressionList = [];
    for (const expression of props.node.expressionList) {
      if (!expression.column) {
        return {
          valid: false,
          message: "请选择字段"
        };
      }
      if (!expression.condition) {
        return {
          valid: false,
          message: "请选择条件"
        };
      }
      const value = "'" + expression.value + "'";
      switch (expression.condition) {
        case "eq":
          expressionList.push(expression.column + "==" + value);
          break;
        case "neq":
          expressionList.push(expression.column + "!=" + value);
          break;
        case "gt":
          expressionList.push(expression.column + ">" + value);
          break;
        case "geq":
          expressionList.push(expression.column + ">=" + value);
          break;
        case "lt":
          expressionList.push(expression.column + "<" + value);
          break;
        case "leq":
          expressionList.push(expression.column + "<=" + value);
          break;
        case "like":
          expressionList.push(expression.column + ".contains(" + value + ")");
          break;
        case "like_left":
          expressionList.push(expression.column + ".startWith(" + value + ")");
          break;
        case "like_right":
          expressionList.push(expression.column + ".endWith(" + value + ")");
          break;
        case "is_null":
        case "not_null":
        default:
          return {
            valid: false,
            message: "条件类型'" + expression.condition + "'无法识别"
          };
      }
    }
    props.node.expression = expressionList.map(expression => "${" + expression + "}").join(props.node.isAnd ? "&&" : "||");
  }
  return {
    valid: valid,
    message: "请选择条件"
  };
});
</script>

<style lang="scss" scoped>
.ep-node-condition {
}
</style>
